import { HashService } from '@app/hash';
import { PrismaService } from '@app/prisma';
import { CommentEntity } from '@app/prisma/entitys/CommentEntity';
import { UserEntity } from '@app/prisma/entitys/user.entity';
import { UseGuards } from '@nestjs/common';
import { Args, Int, Mutation, Query, Resolver } from '@nestjs/graphql';
import { GqlAuthGuard, GqlCurrentUser } from 'src/auth/auth.guard';

@Resolver()
export class CommentResolver {
    constructor(
        private readonly prismaService: PrismaService,
        private readonly hashService: HashService
    ) { }

    @Query(() => [CommentEntity])
    getCommentByShop(
        @Args("id", { type: () => Int }) id: number
    ) {
        return this.prismaService.comment.findMany({
            where: {
                shopId: id
            },
            orderBy: {
                id: "desc"
            },
            include: {
                user: true
            }
        })
    }

    @Query(() => [CommentEntity])
    @UseGuards(GqlAuthGuard)
    getSelfComment(
        @GqlCurrentUser() user: UserEntity
    ) {
        return this.prismaService.comment.findMany({
            where: {
                userId: user.id
            },
            orderBy: {
                id: "desc"
            },
            include: {
                shop: true
            }
        })
    }

    @Query(() => Int)
    async getShopRate(
        @Args("id", { type: () => Int }) id: number
    ) {
        const all = await this.prismaService.comment.findMany({
            where: {
                shopId: id
            }
        })
        if (all.length === 0) return 0;
        return parseFloat((all.reduce((count, item) => count + item.rate, 0) / all.length).toFixed(2))
    }

    @Mutation(() => CommentEntity)
    @UseGuards(GqlAuthGuard)
    async comment(
        @Args("rate", { type: () => Int }) rate: number,
        @Args("context") context: string,
        @Args("id", { type: () => Int }) id: number,
        @GqlCurrentUser() user: UserEntity
    ) {
        const order = await this.prismaService.order.findUniqueOrThrow({
            where: {
                userId: user.id,
                id
            }
        })
        const hash_key = this.hashService.cryptoPassword(`${order.id}_${user.id}`)
        const find = await this.prismaService.comment.findUnique({
            where: {
                hash_key
            }
        })
        if (!!find) {
            return await this.prismaService.comment.update({
                where: {
                    id: find.id
                },
                data: {
                    rate,
                    context
                }
            })
        }
        return await this.prismaService.comment.create({
            data: {
                shopId: order.shopId,
                userId: user.id,
                rate,
                orderId: order.id,
                context,
                hash_key
            }
        })
    }

    @Query(() => [CommentEntity])
    getCommentList() {
        return this.prismaService.comment.findMany({
            orderBy: {
                id: "desc"
            },
            include: {
                user: true,
                shop: true
            }
        })
    }
    @Query(() => [CommentEntity])
    @UseGuards(GqlAuthGuard)
    getSelfCommentList(
        @GqlCurrentUser() user: UserEntity
    ) {
        return this.prismaService.comment.findMany({
            orderBy: {
                id: "desc"
            },
            where: {
                shop: {
                    phone: user.account
                }
            },
            include: {
                user: true,
                shop: true
            }
        })
    }

    @Mutation(() => CommentEntity)
    @UseGuards(GqlAuthGuard)
    delComment(
        @Args("id", { type: () => Int }) id: number
    ) {
        return this.prismaService.comment.delete({
            where: { id }
        })
    }
}
